#include "opponent.h"
#include "..\gameMath.h"
#include "..\gameParameters.h"

Opponent::Opponent(MD2Model *_opponentModel, MD2Model *_weaponModel)
{
	opponentModel = _opponentModel;
	weaponModel = _weaponModel;

	strcpy_s(walkFrame, strlen(walkFrame), "run");
	strcpy_s(runFrame, strlen(runFrame), "run");
	strcpy_s(stayFrame, strlen(stayFrame), "stand");
	strcpy_s(attackFrame, strlen(attackFrame), "attack");
	strcpy_s(hitFrame, strlen(hitFrame), "hit");
	strcpy_s(dieFrame, strlen(dieFrame), "death3");

	if (opponentModel != NULL)
		opponentModel->setAnimation(stayFrame);

	if (weaponModel != NULL)
		weaponModel->setAnimation(stayFrame);

	isStaying = true;
	isWalking = false;
	isAttacking = false;
	isDying = false;
	isBlooding = false;

	stayingAnimationSpeed = 0.003;
	walkingAnimationSpeed = 0.003;
	attackingAnimationSpeed = 0.003;
	bloodingAnimationSpeed = 0.003;

	moves = false;
	life = 4;
}

Opponent::~Opponent()
{
}

void Opponent::init()
{
	posx = pos1x;
	posy = pos1y;
	posz = pos1z;

	deltaPosx = pos2x - pos1x;
	deltaPosy = pos2y - pos1y;
	deltaPosz = pos2z - pos1z;

	speedx = deltaPosx * walkingSpeed;
	speedy = deltaPosy * walkingSpeed;
	speedz = deltaPosz * walkingSpeed;
}

void Opponent::draw()
{
	if (opponentModel != NULL)
		opponentModel->draw();

	if (weaponModel != NULL)
		weaponModel->draw();
}

void Opponent::hit(Player *player)
{
	if (isDying)
		return;

	if (opponentModel != NULL)
	{
		--life;
		if (life == 0)
		{
			isDying = true;
			isStaying = false;
			isWalking = false;
			isAttacking = false;

			opponentModel->dies = true;
			opponentModel->setAnimation(dieFrame);
		}
		else
		{
			isBlooding = true;
			opponentModel->setFinishedAnimation(false);
			opponentModel->setAnimation(hitFrame, currentFrame, true);
		}
	}

	if (weaponModel != NULL)
	{
		if (life == 0)
		{
			isDying = true;
			isStaying = false;
			isWalking = false;
			isAttacking = false;

			weaponModel = NULL;
			//weaponModel->dies = true;
			//weaponModel->setAnimation(dieFrame);
		}
		else
		{
			isBlooding = true;
			weaponModel->setFinishedAnimation(false);
			weaponModel->setAnimation(hitFrame, currentFrame, true);
		}
	}
}

void Opponent::advanceAnimation(Player *player)
{
	checkState(player);
	changePosition();

	if (isDying)
	{
		if (opponentModel != NULL)
			opponentModel->advance(dyingAnimationSpeed);
		if (weaponModel != NULL)
			weaponModel->advance(dyingAnimationSpeed);
	}
	else if (isBlooding)
	{
		if (opponentModel != NULL)
			opponentModel->advance(bloodingAnimationSpeed);
		if (weaponModel != NULL)
			weaponModel->advance(bloodingAnimationSpeed);
	}
	else if (isStaying)
	{
		if (opponentModel != NULL)
			opponentModel->advance(stayingAnimationSpeed);
		if (weaponModel != NULL)
			weaponModel->advance(stayingAnimationSpeed);
	}
	else if (isWalking)
	{
		if (opponentModel != NULL)
			opponentModel->advance(walkingAnimationSpeed);
		if (weaponModel != NULL)
			weaponModel->advance(walkingAnimationSpeed);
	}
	else if (isAttacking)
	{
		if (opponentModel != NULL)
			opponentModel->advance(attackingAnimationSpeed);
		if (weaponModel != NULL)
			weaponModel->advance(attackingAnimationSpeed);
	}
}

void Opponent::setAnimation(char* name, char *frameToReturn, bool returnToPreviousFrame)
{
	if (opponentModel != NULL)
		opponentModel->setAnimation(name, frameToReturn, returnToPreviousFrame);

	if (weaponModel != NULL)
		weaponModel->setAnimation(name, frameToReturn, returnToPreviousFrame);
}

void Opponent::changePosition()
{
	if (moves && isWalking)
	{
		posx += speedx * direction;
		posy += speedy * direction;
		posz += speedz * direction;

		if (posx >= pos2x || posx <= pos1x)
		{
			direction = -direction;
		}
	}
}


void Opponent::setNumber(int value)
{
	number = value;
}

int Opponent::getNumber()
{
	return number;
}
void Opponent::checkDistanceToPlayer(Vector playerPosition)
{
	if (isDying)
		return;

	Vector opponentPosition(posx, posy, posz);
	if (distanceBetweenPoints(playerPosition, opponentPosition) < GameParameters::distanceToPlayerToAttack)
		attack();
	else
	{
		if (moves)
			walk();
		else
			stay();
	}
}

void Opponent::attack()
{
	if (strcmp(currentFrame, attackFrame) != 0)
	{
		opponentModel->setAnimation(attackFrame);
		weaponModel->setAnimation(attackFrame);
		strcpy_s(currentFrame, strlen(currentFrame), attackFrame);

		isWalking = false;
		isAttacking = true;
		isStaying = false;
	}
}

void Opponent::walk()
{
	if (strcmp(currentFrame, walkFrame) != 0)
	{
		opponentModel->setAnimation(walkFrame);
		weaponModel->setAnimation(walkFrame);
		strcpy_s(currentFrame, strlen(currentFrame), walkFrame);

		isWalking = true;
		isAttacking = false;
		isStaying = false;
	}
}

void Opponent::stay()
{
	if (strcmp(currentFrame, stayFrame) != 0)
	{
		opponentModel->setAnimation(stayFrame);
		weaponModel->setAnimation(stayFrame);
		strcpy_s(currentFrame, strlen(currentFrame), stayFrame);

		isWalking = false;
		isAttacking = false;
		isStaying = true;
	}
}

void Opponent::checkState(Player *player)
{
	//if (opponentModel != NULL)
	//{
	//	if (opponentModel->getFinishedAnimation())
	//	{
	//		opponentModel->setFinishedAnimation(false);
	//		isBlooding = false;
	//		if (isAttacking)
	//			player->damage(5);
	//	}
	//}
}